Skip to content

fix: comment limit is 29 UTF-16 units, as the MetaTrader5 package enforces - #15

Merged
Akinzou merged 1 commit into
masterfrom
fix/comment-limit
Nov 9, 2026
Merged

Akinzou merged 1 commit into
masterfrom
fix/comment-limit

Conversation

@Akinzou

@Akinzou Akinzou commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Goal

The first run of pytest -m integration on a demo account (MetaQuotes-Demo, MetaTrader5 5.0.6231) showed that the comment limit in 3.0 was wrong. The library let 30–31 characters through, but the MetaTrader5 package refuses them with [-2] Invalid "comment" argument. The caller got a generic TERMINAL_ERROR instead of INVALID_ARGUMENT. Nothing was sent.

What changed

  • Comment limit: 29 UTF-16 units, measured with order_check, which sends nothing.
    • 29 units pass and 30 are refused, for ASCII, Polish, CJK and emoji alike.
    • "🚀" * 15 (15 characters, 30 units) is refused, so len() was the wrong measure.
    • A lone surrogate is accepted by the package and counts as one unit.
  • Non-text comment: a comment that is not a str is refused as INVALID_ARGUMENT. Before, comment or "" turned 0 / False into an empty comment without a word.
  • New integration test: it re-measures the boundary against the installed package with order_check and asserts that the refusal is the comment error (-2), so a package that moves the limit fails it. Verified: with the documented 31 it fails 2 of 4 cases.
  • Updated test: the market-position integration test now opens with a 29-unit comment, so the accept side is proven on a real order.
  • Docs: README, MIGRATION and CHANGELOG are updated.

Demo-account verification (the three open questions from #14)

  • Filling for limit orders on a market-execution broker: OK. EURUSD on MetaQuotes-Demo allows only FOK with market execution, and the limit order was placed and cancelled.
  • Comment limit: it was 31 in our code but 29 in reality. This PR fixes it.
  • positions_get / orders_get: () with last_error Success for an empty result, and None with -4 Not found for an unknown symbol. The adapter already maps these to Ok([]) and Err.

Verification

  • 312 tests pass, 99% coverage. ruff, ruff format, mypy --strict and the file-length check are clean.

  • On the demo account, pytest -m integration passes 7 of 7.

  • Two /code-review high rounds. Accepted findings:

    • UTF-16 counting;
    • test boundaries placed exactly at 29/30;
    • the refusal checked as the -2 comment error;
    • error wording;
    • object parameter type.

    Refuted by measurement: the claim that a lone surrogate raises UnicodeEncodeError.

Named debt (not fixed here)

  • The MetaTrader5 dependency is not pinned. A build that moves the limit would make us refuse valid comments (30–31) or let invalid ones through to TERMINAL_ERROR. Neither case sends a bad order. The integration test detects the drift, but only where someone runs it with a demo account.
  • The adapter's _send_failure maps -2 (RES_E_INVALID_PARAMS) to TERMINAL_ERROR. Every argument the package refuses is therefore misclassified, not only the comment. A separate change, because it touches the not-sent / outcome-unknown classification.
  • MAX_COMMENT_LENGTH is not public API.

Semgrep

Re-checked by CI on this push.

…orces

Measured on a demo account with order_check (sends nothing), MetaTrader5
5.0.6231: 29 UTF-16 units pass, 30 get [-2] Invalid "comment" argument,
for ASCII, Polish, CJK and emoji alike; "\U0001f680" * 15 (15 characters,
30 units) is refused, so len() was the wrong measure. The documented 31
let 30-31 through to a TERMINAL_ERROR that hid the cause.

A comment that is not str is refused as INVALID_ARGUMENT: `comment or ""`
stored 0 or False as an empty comment without a word.

tests/integration re-measures the boundary against the installed package
with order_check and asserts the refusal is the comment error (-2), so a
package that moves the limit fails there.

RED: test_orders (30 units, emoji, non-str) and test_failure_paths failed
against the old limit; the integration boundary test failed with
MAX_COMMENT_LENGTH = 31 (2 of 4 cases).

Renamed tests test_comment_within_terminal_limit_is_kept and
test_comment_over_terminal_limit_is_rejected_not_truncated. Repo-wide grep
(excluding .venv/.git): matches only in .pytest_cache/.mypy_cache.
@Akinzou
Akinzou merged commit 4b0c97c into master Nov 9, 2026
8 checks passed
@Akinzou
Akinzou deleted the fix/comment-limit branch November 14, 2026 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant